home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter10 / isohex10_1 / isohex10_1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-20  |  6.4 KB  |  260 lines

  1. /*****************************************************************************
  2. IsoHex10_1.cpp
  3. Ernest S. Pazera
  4. 19JUN2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Needs ddraw.lib and dxguid.lib
  7. Needs GDICanvas.h/cpp
  8. Needs DDFuncs.h/cpp
  9. Art by Ari Feldman
  10. *****************************************************************************/
  11.  
  12. //////////////////////////////////////////////////////////////////////////////
  13. //INCLUDES
  14. //////////////////////////////////////////////////////////////////////////////
  15. #define WIN32_LEAN_AND_MEAN  
  16.  
  17. #include <windows.h>
  18. #include "GDICanvas.h"
  19. #include "ddraw.h"
  20. #include "DDFuncs.h"
  21. #include "TileSet.h"
  22.    
  23. //////////////////////////////////////////////////////////////////////////////
  24. //DEFINES
  25. //////////////////////////////////////////////////////////////////////////////
  26. //name for our window class
  27. #define WINDOWCLASS "ISOHEX10"
  28. //title of the application
  29. #define WINDOWTITLE "IsoHex 10-1, with art by Ari Feldman"
  30.  
  31. //////////////////////////////////////////////////////////////////////////////
  32. //PROTOTYPES
  33. //////////////////////////////////////////////////////////////////////////////
  34. bool Prog_Init();//game data initalizer
  35. void Prog_Loop();//main game loop
  36. void Prog_Done();//game clean up
  37.  
  38. //////////////////////////////////////////////////////////////////////////////
  39. //GLOBALS
  40. //////////////////////////////////////////////////////////////////////////////
  41. HINSTANCE hInstMain=NULL;//main application handle
  42. HWND hWndMain=NULL;//handle to our main window
  43.  
  44. //directdraw
  45. LPDIRECTDRAW7 lpdd=NULL;
  46. LPDIRECTDRAWSURFACE7 lpddsMain=NULL;
  47. LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
  48. CTileSet tsCaveMan;
  49. DWORD dwCaveManFrame=0;
  50.  
  51. //////////////////////////////////////////////////////////////////////////////
  52. //WINDOWPROC
  53. //////////////////////////////////////////////////////////////////////////////
  54. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  55. {
  56.     //which message did we get?
  57.     switch(uMsg)
  58.     {
  59.     case WM_KEYDOWN:
  60.         {
  61.             //on escape, destroy main window
  62.             if(wParam==VK_ESCAPE)
  63.             {
  64.                 DestroyWindow(hWndMain);
  65.             }
  66.  
  67.             return(0);//handled
  68.         }break;
  69.     case WM_DESTROY://the window is being destroyed
  70.         {
  71.  
  72.             //tell the application we are quitting
  73.             PostQuitMessage(0);
  74.  
  75.             //handled message, so return 0
  76.             return(0);
  77.  
  78.         }break;
  79.     case WM_PAINT://the window needs repainting
  80.         {
  81.             //a variable needed for painting information
  82.             PAINTSTRUCT ps;
  83.             
  84.             //start painting
  85.             HDC hdc=BeginPaint(hwnd,&ps);
  86.  
  87.             /////////////////////////////
  88.             //painting code would go here
  89.             /////////////////////////////
  90.  
  91.             //end painting
  92.             EndPaint(hwnd,&ps);
  93.                         
  94.             //handled message, so return 0
  95.             return(0);
  96.         }break;
  97.     }
  98.  
  99.     //pass along any other message to default message handler
  100.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  101. }
  102.  
  103.  
  104. //////////////////////////////////////////////////////////////////////////////
  105. //WINMAIN
  106. //////////////////////////////////////////////////////////////////////////////
  107. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  108. {
  109.     //assign instance to global variable
  110.     hInstMain=hInstance;
  111.  
  112.     //create window class
  113.     WNDCLASSEX wcx;
  114.  
  115.     //set the size of the structure
  116.     wcx.cbSize=sizeof(WNDCLASSEX);
  117.  
  118.     //class style
  119.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  120.  
  121.     //window procedure
  122.     wcx.lpfnWndProc=TheWindowProc;
  123.  
  124.     //class extra
  125.     wcx.cbClsExtra=0;
  126.  
  127.     //window extra
  128.     wcx.cbWndExtra=0;
  129.  
  130.     //application handle
  131.     wcx.hInstance=hInstMain;
  132.  
  133.     //icon
  134.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  135.  
  136.     //cursor
  137.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  138.  
  139.     //background color
  140.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  141.  
  142.     //menu
  143.     wcx.lpszMenuName=NULL;
  144.  
  145.     //class name
  146.     wcx.lpszClassName=WINDOWCLASS;
  147.  
  148.     //small icon
  149.     wcx.hIconSm=NULL;
  150.  
  151.     //register the window class, return 0 if not successful
  152.     if(!RegisterClassEx(&wcx)) return(0);
  153.  
  154.     //create main window
  155.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  156.  
  157.     //error check
  158.     if(!hWndMain) return(0);
  159.  
  160.     //if program initialization failed, then return with 0
  161.     if(!Prog_Init()) return(0);
  162.  
  163.     //message structure
  164.     MSG msg;
  165.  
  166.     //message pump
  167.     for(;;)    
  168.     {
  169.         //look for a message
  170.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  171.         {
  172.             //there is a message
  173.  
  174.             //check that we arent quitting
  175.             if(msg.message==WM_QUIT) break;
  176.             
  177.             //translate message
  178.             TranslateMessage(&msg);
  179.  
  180.             //dispatch message
  181.             DispatchMessage(&msg);
  182.         }
  183.  
  184.         //run main game loop
  185.         Prog_Loop();
  186.     }
  187.     
  188.     //clean up program data
  189.     Prog_Done();
  190.  
  191.     //return the wparam from the WM_QUIT message
  192.     return(msg.wParam);
  193. }
  194.  
  195. //////////////////////////////////////////////////////////////////////////////
  196. //INITIALIZATION
  197. //////////////////////////////////////////////////////////////////////////////
  198. bool Prog_Init()
  199. {
  200.     //create IDirectDraw7
  201.     lpdd=LPDD_Create(hWndMain,DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);
  202.  
  203.     //set display mode
  204.     lpdd->SetDisplayMode(800,600,16,0,0);
  205.  
  206.     //create primary surface
  207.     lpddsMain=LPDDS_CreatePrimary(lpdd,1);
  208.  
  209.     //get back buffer
  210.     lpddsBack=LPDDS_GetSecondary(lpddsMain);
  211.  
  212.     //clear out back buffer
  213.     DDBLTFX ddbltfx;
  214.     DDBLTFX_ColorFill(&ddbltfx,0);
  215.     lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
  216.  
  217.     //load in tileset
  218.     tsCaveMan.Load(lpdd,"IsoHex10_1.bmp");
  219.  
  220.     return(true);//return success
  221. }
  222.  
  223. //////////////////////////////////////////////////////////////////////////////
  224. //CLEANUP
  225. //////////////////////////////////////////////////////////////////////////////
  226. void Prog_Done()
  227. {
  228.     //destroy primary surface
  229.     LPDDS_Release(&lpddsMain);
  230.  
  231.     //destroy IDirectDraw7
  232.     LPDD_Release(&lpdd);
  233. }
  234.  
  235. //////////////////////////////////////////////////////////////////////////////
  236. //MAIN GAME LOOP
  237. //////////////////////////////////////////////////////////////////////////////
  238. void Prog_Loop()
  239. {
  240.     //start timer
  241.     DWORD dwTimeStart=GetTickCount();
  242.  
  243.     //clear out back buffer
  244.     DDBLTFX ddbltfx;
  245.     DDBLTFX_ColorFill(&ddbltfx,0);
  246.     lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
  247.  
  248.     //put the caveman
  249.     tsCaveMan.PutTile(lpddsBack,400,300,dwCaveManFrame);
  250.     //change the frame number
  251.     dwCaveManFrame++;
  252.     dwCaveManFrame%=8;
  253.     //flip
  254.     lpddsMain->Flip(NULL,DDFLIP_WAIT);
  255.  
  256.     //lock to 15 FPS
  257.     while(GetTickCount()-dwTimeStart<66);
  258. }
  259.  
  260.